
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade.helpers;

import java.util.ArrayList;
import java.util.List;

import javax.management.Attribute;
import javax.management.ObjectName;

import oxygen.tool.wlfacade.WLFacadeContextHelper;
import oxygen.tool.wlfacade.WLMBSTree;
import oxygen.tool.wlfacade.WLScriptHelper;

// these actions (except invoke) can only be done on the AS
public class WLConfig {
  // you can invoke something on a runtime mbean
  public static Object invoke(WLFacadeContextHelper wlctx, String opname, List args) 
    throws Exception {
    if(args == null) {
      args = new ArrayList(0);
    }
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    return invoke(wlctx, (ObjectName)wltree.pointer(), opname, args.toArray());
  }
    
  public static Object invoke(WLFacadeContextHelper wlctx, String opname, Object[] args) 
    throws Exception {
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    return invoke(wlctx, (ObjectName)wltree.pointer(), opname, args);
  }    
    
  public static Object invoke(WLFacadeContextHelper wlctx, String opname) 
    throws Exception {
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    return invoke(wlctx, (ObjectName)wltree.pointer(), opname, new Object[0]);
  }    
    
  // you can get an attribute of a runtime mbean
  public static Object getatt(WLFacadeContextHelper wlctx, String attname) 
    throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    Object mbs = wltree.getMBS();
    ObjectName cmo = (ObjectName)wltree.pointer();
    Object attval = WLScriptHelper.mbs_getAttribute(mbs, cmo, attname);
    return attval;
    //System.out.println("Attribute value got");
  }

  public static String getattstr(WLFacadeContextHelper wlctx, String attname) 
    throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensurePrimaryDomain(wlctx);
    WLScriptHelper.ensureNotDomainRoot(wlctx);
    Object obj = getatt(wlctx, attname);
    return WLConfig2.getattstr(wlctx, attname, obj);
  }

  
  public static void setatt(WLFacadeContextHelper wlctx, String attname, Object attval) 
    throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureAdminServer(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    Object mbs = wltree.getMBS();
    ObjectName woname = (ObjectName)wltree.pointer();
    //woname = WLScriptHelper.getCorrespondingAdminObjectName(wlctx, woname);
    Attribute att = new Attribute(attname, attval);
    WLScriptHelper.mbs_setAttribute(mbs, woname, att);
    //System.out.println("Attribute value set");
  }

  public static void setattstr(WLFacadeContextHelper wlctx, String attname, String attval) 
    throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensurePrimaryDomain(wlctx);
    WLScriptHelper.ensureNotDomainRoot(wlctx);
    Object attvalobj = WLConfig2.setattstr(wlctx, attname, attval);
    setatt(wlctx, attname, attvalobj);
  }
  
  // you can only create an mbean on an admin server
  // only works for the primary domain
//  public static ObjectName create(WLFacadeContextHelper wlctx, String arg) 
//    throws Exception {
//    WLScriptHelper.ensureConnected(wlctx);
//    WLScriptHelper.ensureAdminServer(wlctx);
//    WLScriptHelper.ensurePrimaryDomain(wlctx);
//    //WLScriptHelper.ensureNotDomainRoot(wlctx);
//    WLScriptHelper.ensureMBSTree(wlctx);
//    
//    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
//    String domain = wltree.getDomain();
//    WLJMXHandler dmhdlr = wltree.getJMXHandler();
//    ObjectName cmo = (ObjectName)wltree.pointer();
//    ObjectName pattern = dmhdlr.getLookupPattern(domain, arg);
//    MBeanServer mbs = wlctx.getConnection().getChangeMBS();
//    ObjectName cmo2 = null;
//    try {
//      if(cmo == null) {
//        cmo2 = WLScriptHelper.getSingleObjName(mbs, domain, arg, dmhdlr, null, true, null);
//      } else {
//        cmo2 = WLScriptHelper.getSingleObjName(mbs, domain, arg, dmhdlr, cmo, false, null);
//      }
//    } catch(Exception exc) {
//      //means that that MBean does not exist, so we should create it
//      System.err.println("Exception handled and ignored: " + exc);
//      //exc.printStackTrace();
//    }
//    if(cmo2 == null) {
//      WLJMXHandler.TypeName typenamecombo = dmhdlr.getTypeName(arg);
//      cmo2 = wlctx.getConnection().createMBean(cmo, typenamecombo.type, typenamecombo.name);
//    }
//    return cmo2;
//    //System.out.println("MBean created");
//  }

//  public static void delete(WLFacadeContextHelper wlctx, String arg) 
//    throws Exception {
//    WLScriptHelper.ensureConnected(wlctx);
//    WLScriptHelper.ensureAdminServer(wlctx);
//    WLScriptHelper.ensurePrimaryDomain(wlctx);
//    WLScriptHelper.ensureMBSTree(wlctx);
//    
//    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
//    String domain = wltree.getDomain();
//    WLJMXHandler dmhdlr = wltree.getJMXHandler();
//    ObjectName pattern = dmhdlr.getLookupPattern(domain, arg);
//    ObjectName cmo = (ObjectName)wltree.pointer();
//    MBeanServer mbs = wlctx.getConnection().getChangeMBS();
//    if(cmo == null) {
//      cmo = WLScriptHelper.getSingleObjName(mbs, domain, arg, dmhdlr, null, true, null);
//    } else {
//      cmo = WLScriptHelper.getSingleObjName(mbs, domain, arg, dmhdlr, cmo, false, null);
//    }
//    wlctx.getConnection().deleteMBean(cmo);
//    //System.out.println("MBean deleted");
//  }

  public static Object mbean(WLFacadeContextHelper wlctx) throws Exception {
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    return mbean(wlctx, (ObjectName)wltree.pointer());
  }

  public static Object mbean(WLFacadeContextHelper wlctx, String stringrep) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureNotDomainRoot(wlctx);
    return WLScriptHelper.tombean(wlctx, stringrep);
  }

  public static Object mbean(WLFacadeContextHelper wlctx, ObjectName cmo) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureNotDomainRoot(wlctx);
    return WLScriptHelper.tombean(wlctx, cmo);
  }

  public static void savedomain(WLFacadeContextHelper wlctx) throws Exception {
    String priDomainName = wlctx.getConnection().getDomainName();
    //RepositoryMBean rep = (RepositoryMBean)adminHome.getMBean("Default", "Repository", WebLogicObjectName.WEBLOGIC);
    //rep.saveDomain(priDomainName);
    ObjectName cmo = WLScriptHelper.getObjName(wlctx, "weblogic:/Default");
    invoke(wlctx, cmo, "saveDomain", new Object[]{priDomainName});
  }
  
  public static Object mbs(WLFacadeContextHelper wlctx) throws Exception {
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    return wltree.getMBS();
  }
  
  private static Object invoke(WLFacadeContextHelper wlctx, ObjectName cmo, String opname, Object[] args) 
    throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureNotDomainRoot(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    if(args == null) {
      args = new Object[0];
    }
    Object mbs = wltree.getMBS();
    Object obj = WLScriptHelper.mbs_invoke(mbs, cmo, opname, args);
    return obj;
    //System.out.println("Invoke completed");
  } 

}

